package ro.ubbcluj.cs.validation.framework.tags.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.ubbcluj.cs.validation.framework.tags.BaseTag;
import ro.ubbcluj.cs.validation.framework.tags.FormTag;

public final class TagUtils {
	
	private static Logger log = LoggerFactory.getLogger(TagUtils.class);
	
	public static final String getFormName(BaseTag inputTag){
		if (inputTag.getParent() instanceof FormTag) {
			String name = ((FormTag)inputTag.getParent()).getName();
			return name != null ? name+":" : "";
		} else {
			log.error("Wrong place for the input tag! Input tag can be put only in the form.");
			return "";
		}
	}
	
	public static final String normalizeName(String name) {
		String normalizedName = name.replaceAll("[\\W]+", "-");
		if (normalizedName.startsWith("-")) {
			normalizedName = normalizedName.substring(1);
		}
		if (normalizedName.endsWith("-")) {
			normalizedName = normalizedName.substring(0, normalizedName.length()-1);
		}
		return normalizedName;
	}
}
